package com.tos.second.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.tos.second.common.Result.PageResult;
import com.tos.second.common.Result.Result;
import com.tos.second.common.basecontext.BaseContext;
import com.tos.second.dto.GoodsAddDto;
import com.tos.second.dto.GoodsBuyDto;
import com.tos.second.dto.GoodsChangeDto;
import com.tos.second.dto.GoodsSearchDto;
import com.tos.second.entity.Goods;
import com.tos.second.service.GoodsService;
import com.tos.second.vo.GoodsGetVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")

@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查询所有商品
    @GetMapping("/list")
    public Result<List<Goods>> list(){
        return Result.success(goodsService.list());
    }
    /**
     * 根据分类id获得商品
     * @param categoryId
     * @return
     */

    @GetMapping("/page/{categoryId}")
    public Result<PageResult> page(@PathVariable Long categoryId, Integer pageNum, Integer pageSize){
        log.info("根据分类id获得商品:{}",categoryId);
        PageResult p = goodsService.getPage(categoryId,pageNum,pageSize);
        return Result.success(p);
    }


    /**
     * 添加商品
     * @param goodsAddDto
     * @return
     */
    @PostMapping("/addGoods")
    public Result<String> addGoods(@RequestBody GoodsAddDto goodsAddDto){
        log.info("添加商品接口:{}",goodsAddDto.toString());
        goodsService.addGoods(goodsAddDto);
        return Result.success("添加成功");
    }


    /**
     * 根据id获得商品
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<GoodsGetVo> getGoodsById(@PathVariable Long id){
        log.info("根据id获得商品:{}",id);
        GoodsGetVo goodsGetByIdVo = goodsService.getGoodsById(id);
        return Result.success(goodsGetByIdVo);
    }

    /**
     * 购买商品
     * @param id
     * @return
     */
    @PostMapping("/buy/{id}")
    public Result buy(@PathVariable Long id, @RequestBody GoodsBuyDto goodsBuyDto){
        log.info("购买商品接口:{}",id);
        String number =goodsService.buy(id,goodsBuyDto);
        //使用map集合
        Map<Integer, String> map = Map.of(1,number);
        return Result.success(map);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        log.info("删除商品接口:{}",id);
        goodsService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 商品上下架
     * @param status
     * @return
     */
    @PutMapping("/status/{status}")
    public Result<String> startOrStopById(@PathVariable Integer status,Long id){
        log.info("商品上下架接口:{}",id);
        goodsService.startOrStopById(status,id);
        return Result.success("操作成功");
    }


    /**
     * 获得最新商品
     * @return
     */
    @GetMapping("/getNewGoods")
    @Cacheable(cacheNames = "newGoodsCache")
    public Result<List<Goods>> getNewGoods(){
        log.info("获取最新商品");
        List<Goods> goodsGetNewVos = goodsService.getNewGoods();
        return Result.success(goodsGetNewVos);
    }

    /**
     * 获得我的商品
     * @return
     */
    @GetMapping("/getMyGoods")
    public Result<List<Goods>> getMyGoods(){
        log.info("获取我的商品");
        List<Goods> goodsGetMyVos = goodsService.getMyGoods();
        return Result.success(goodsGetMyVos);
    }

    /**
     * 修改
     * @param goodsChangeDto
     * @return
     */
    @PutMapping("/changeGoods")
    public Result<String> changeGoodsMsg(@RequestBody GoodsChangeDto goodsChangeDto){
        log.info("修改商品信息:{}",goodsChangeDto.toString());
        goodsService.changeGoodsMsg(goodsChangeDto);
        return Result.success("修改成功");
    }

    /**
     * 获得商品
     * @param goodsSearchDto
     * @return
     */
    @PostMapping("/searchByName")
    public Result<PageResult> getGoodsByName(@RequestBody GoodsSearchDto goodsSearchDto){
        log.info("根据商品名查询商品:{}",goodsSearchDto.toString());
        PageResult goodsGetByNameVos = goodsService.getGoodsByName(goodsSearchDto);
        return Result.success(goodsGetByNameVos);
    }


    /**
     * 获得推荐商品
     * @return
     */
    @GetMapping("/getRecGoods")
    public Result<List<Goods>> getRecGoods(){
        log.info("获得推荐商品");
        List<Goods> goodsGetRecVos = goodsService.getRecGoods();
        return Result.success(goodsGetRecVos);
    }



    @GetMapping("/home/{categoryId}")
    @Cacheable(cacheNames = "home",key = "#categoryId")
    public Result<List<Goods>> getHome(@PathVariable Long categoryId){
        log.info("获取首页商品:{}",categoryId);
        List<Goods> goodsList = goodsService.getHomeList(categoryId);
        return Result.success(goodsList);

    }

    @GetMapping("/getByCategory/{categoryId}")
    public Result<List<Goods>> getByCategory(@PathVariable Long categoryId){
        log.info("获取分类商品:{}",categoryId);
        List<Goods> goodsList = goodsService.listById(categoryId);
        return Result.success(goodsList);

    }


}